package com.fankui.crowd.mvc.handler;

import com.fankui.crowd.entity.Role;
import com.fankui.crowd.service.RoleService;
import com.fankui.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName RoleHandler
 * Description TODO
 * Date 2023/7/30 22:14
 *
 * @Author 范奎
 * @Since JDK 11
 */
@RestController
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    @PreAuthorize("hasRole('角色管理员') or hasAuthority('role:get')")
    @RequestMapping("/role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getRolePage(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<Role> rolePageInfo = roleService.getRolePage(keyword, pageNum, pageSize);
        return ResultEntity.successWithData(rolePageInfo);
    }

    @PreAuthorize("hasAuthority('role:add')")
    @RequestMapping("/role/save.json")
    public ResultEntity<String> saveRole(Role role) {
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasAuthority('role:update')")
    @RequestMapping("/role/update.json")
    public ResultEntity<String> updateRole(Role role) {
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasAuthority('role:delete')")
    @RequestMapping("/role/remove/role/by/id/array.json")
    public ResultEntity<String> removeRoleByIdArray(@RequestBody List<Integer> roleIdList) {
        roleService.removeRole(roleIdList);
        return ResultEntity.successWithoutData();
    }
}
